package org.idea.qiyu.framework.jmh.demo;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

/**
 * @Author linhao
 * @Date created in 10:20 上午 2021/12/19
 */
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@Threads(4)
@Warmup(iterations = 1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class ForLoopDemo {

    public int reps(int count) {
        int sum = 0;
        for (int i = 0; i < count; i++) {
            sum = sum + count;
        }
        return sum;
    }

    @Benchmark
    @OperationsPerInvocation(1)
    public int test_1() {
        return reps(1);
    }

    @Benchmark
    @OperationsPerInvocation(10)
    public int test_2() {
        return reps(10);
    }

    @Benchmark
    @OperationsPerInvocation(100)
    public int test_3() {
        return reps(100);
    }

    @Benchmark
    @OperationsPerInvocation(1000)
    public int test_4() {
        return reps(1000);
    }

    @Benchmark
    @OperationsPerInvocation(10000)
    public int test_5() {
        return reps(10000);
    }

    @Benchmark
    @OperationsPerInvocation(100000)
    public int test_6() {
        return reps(100000);
    }

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(ForLoopDemo.class.getName())
                .build();
        new Runner(options).run();
    }
}
